package com.realgodo.site.service.site;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.site.Advertisement;

@Transactional
@Service("advertisementManager")
public class AdvertisementManager extends
		DefaultEntityManager<Advertisement, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	/**
	 * 广告查询
	 */
	public Page<Advertisement> findAdvertisements(
			Page<Advertisement> advertisementPage) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Advertisement.class);
		c.addOrder(Order.desc("id"));
		return this.entityDao.findByCriteria(advertisementPage, c);
	}

	public Advertisement getByCode(final String code) {
		return this.entityDao.findByUnique("code", code);
	}
}
